begintownscript;

variables;
short choice;
short i, safety, pc_counter;
short ingate;
short npc_counter;

body;

beginstate 0; //INIT_STATE

	//intro dialog
	if(get_flag(10,0) == 0) {
		reset_dialog();
		add_dialog_str(0,"At last, you can see the front gate of Copperpeak. Now you just have to find a way to get in.",0);
		add_dialog_str(1,"Groups of Loyalists are camped out behind stands of trees, just out of crossbow range. Looks like they've been here for a while now.",0);
		add_dialog_str(2,"You'll have to get past them one way or another if you want to reach the gate.",0);
		run_dialog(1);
		
		set_flag(10,0,1);
		}
	
	//set up groups
	add_range_to_group(6,23,1); //loyalists
	
	add_range_to_group(24,27,2); //guards
	
	//friendly until proven otherwise
	set_attitude(1001,4);
	
break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE
	//Checking for the lead pc each turn... not necessary here
	//pc_counter = 0;
	//while(char_ok(pc_counter) == 0) {
	//	pc_counter = pc_counter + 1;
	//	}
	//lead_char = pc_counter;
	
	//check the gate...
	// if all the PCs are inside the gate, close it
	if(get_flag(10,6) == 1) {
		ingate = 1;
		pc_counter = 0;
		while(pc_counter < 5) {
			if((char_ok(pc_counter) == 1) && ((char_loc_x(pc_counter) < 29) || (char_loc_x(pc_counter) > 31) || (char_loc_y(pc_counter) < 3) || (char_loc_y(pc_counter) > 6)))
				ingate = 0;
			pc_counter = pc_counter + 1;
			}
		if(ingate == 1) {
			play_sound(-99);
			message_dialog("You hear a rattling of chains, and the outer gate closes.","");
			flip_terrain(29,6);
			flip_terrain(30,6);
			flip_terrain(31,6);
			set_flag(10,6,2);
			}
		}
	//now, if the gate is closed, and no NPCs are inside, open
	// the other side
	if(get_flag(10,6) == 2) {
		ingate = 1;
		npc_counter = 6;
		while(npc_counter < 19) {
			if((char_ok(npc_counter) == 1) && ((char_loc_x(npc_counter) > 28) && (char_loc_x(npc_counter) < 32) && (char_loc_y(npc_counter) > 3) && (char_loc_y(npc_counter) < 7)))
				ingate = 0;
			npc_counter = npc_counter + 1;
			}
		if(ingate == 0) {
			if(get_flag(10,8) == 0) {
				reset_dialog();
				add_dialog_str(0,"One of the guards points at you and shouts.",0);
				add_dialog_str(1,"_Clear the gate, and we'll let you in!_",15);
				run_dialog(1);
				set_flag(10,8,1);
				}
			end();
			}
		if(ingate == 1) {
			play_sound(-99);
			message_dialog("You hear a rattling of chains, and the inner gate opens.","");
			flip_terrain(29,4);
			flip_terrain(30,4);
			flip_terrain(31,4);
			set_flag(10,6,3);
			}
		}
break;

beginstate 10; //seeing the Loyalists and ballistas

	if(get_flag(10,1) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"Upon closer examination, it looks like the Loyalists have set up a semi-permanent camp here. This is clearly a well-organized effort.",0);
	add_dialog_str(1,"What's more alarming, however, is the presence of several large, well-maintained siege weapons.",0);
	run_dialog(1);
	
	
	set_flag(10,1,1);

break;

beginstate 20; //keep you from straying too far north
	block_entry(1);
break;

beginstate 30; //force talk with the guards
	if(get_flag(10,4) == 1)
		end();
	
	//check to see if armor is equipped
	pc_counter = 0;
	while((pc_counter < 4) && (safety == 0)) {
		if((char_ok(pc_counter) == 1) && (item_type_in_slot(pc_counter,0) != 447))
			safety = 1; //not safe if armor isn't equipped
		pc_counter = pc_counter + 1;
		}
	
	//pass safety variable to dlg script via sdf
	if(safety == 1)
		set_flag(10,5,2);
	else
		set_flag(10,5,1);
	
	reset_dialog();
	add_dialog_str(0,"Finally! You're close enough to the gates that you can see the guards patrolling the walls.",0);
	add_dialog_str(1,"In fact, it looks like one of the guards is pointing at you...",0);
	run_dialog(1);
	
	set_flag(10,4,1);
	
	begin_talk_mode(10);
break;

beginstate 31; //open fire
	if((get_flag(10,6) == 1) || (get_flag(10,7) == 1))
		end();
	
	//check to see if armor is equipped
	pc_counter = 0;
	while((pc_counter < 4) && (safety == 0)) {
		if((char_ok(pc_counter) == 1) && (item_type_in_slot(pc_counter,0) != 447))
			safety = 1; //not safe if armor isn't equipped
		pc_counter = pc_counter + 1;
		}
	
	if(safety == 0) {
		message_dialog("As promised, the guards open fire on you. Maybe if you took off the Loyalists' armor, they would believe your story.","");
		set_attitude(1002,11);
		set_flag(10,7,1);
		}
		
	else
		begin_talk_mode(13);
	
break;

beginstate 32; //trigger from pickyguard
	begin_talk_mode(14);
break;

beginstate 40; //move to Stonesthrow
	move_to_new_town(6,24,35);
break;